﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LibraryO2S;
using BusinessObjects.BO_GetObjects;
using System.Data.OleDb;
using Entity.StoreProcedure;
using System.Collections.Generic;
using BusinessObjects.BO_Actions.Regions;
using BusinessObjects;
using System.Text;

public partial class controls_admin_Regions : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadRegion();
            lbRegion_SelectedIndexChanged(null, null);
            ddlProvince_SelectedIndexChanged(null, null);
        }
        String hiddenFieldValue = Request["theHiddenTabIndex"] ?? "0";
        StringBuilder js = new StringBuilder();
        js.Append("<script type='text/javascript'>");
        js.Append("var previouslySelectedTab = ");
        js.Append(hiddenFieldValue);
        js.Append(";</script>");
        ltrScript.Text = js.ToString();
        //this.Controls.Add(new LiteralControl(js.ToString()));
    }
    private void loadRegion()
    {
        ddlRegion.Items.Clear();
        ddlProvince.Items.Clear();
        ddlRegion.Items.Add(new ListItem("Tỉnh thành mới", "0"));
        objDT = BO_Regions.SearchByParent(0);
        ddlRegion.DataSource = objDT;
        ddlRegion.DataTextField = "REGION_NAME_VN";
        ddlRegion.DataValueField = "REGION_ID";
        ddlRegion.DataBind();
        ddlProvince.DataSource = objDT;
        ddlProvince.DataTextField = "REGION_NAME_VN";
        ddlProvince.DataValueField = "REGION_ID";
        ddlProvince.DataBind();
    }
    protected void lbRegion_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (ddlRegion.SelectedIndex != 0)
            {
                btnDel.Visible = true;
                objDT = BO_Regions.Search(Convert.ToInt32(ddlRegion.SelectedValue));
                txtregions.Text = objDT.Rows[0]["REGION_NAME_VN"].ToString();
            }
            else
            {
                btnDel.Visible = false;
                txtregions.Text = "";
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable admin = (DataTable)Session["Admin"];
            string regionName = txtregions.Text.Trim();
            if (ddlRegion.SelectedIndex != 0)
            {
                int regionID = Convert.ToInt32(ddlRegion.SelectedValue);
                objDT = BO_Regions.Search(regionID);
                BO_Regions.Update(regionID,
                    Convert.ToInt32(objDT.Rows[0]["PARENT_ID"]), regionName, "",
                    1,
                   Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]), objDT.Rows[0]["CREATED_BY"].ToString(),
                    DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), true);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Sửa thành công !");
            }
            else
            {
                BO_Regions.Insert(
                    0, regionName, "",
                    1,
                    DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(),
                    DateTime.Now, "", true);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thêm thành công !");
            }
            loadRegion();
            lbRegion_SelectedIndexChanged(null, null);
            ddlProvince_SelectedIndexChanged(null, null);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ddlDistrict.Items.Clear();
            ddlDistrict.Items.Add(new ListItem("Địa điểm mới", ddlProvince.SelectedValue));
            objDT = BO_Regions.SearchByParent(Convert.ToInt32(ddlProvince.SelectedValue));
            ddlDistrict.DataSource = objDT;
            ddlDistrict.DataTextField = "REGION_NAME_VN";
            ddlDistrict.DataValueField = "REGION_ID";
            ddlDistrict.DataBind();
            ddlDistrict_SelectedIndexChanged(null, null);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (ddlDistrict.SelectedIndex != 0)
            {
                btnDistrictDel.Visible = true;
                objDT = BO_Regions.Search(Convert.ToInt32(ddlDistrict.SelectedValue));
                txtDistrict.Text = objDT.Rows[0]["REGION_NAME_VN"].ToString();
            }
            else
            {
                btnDistrictDel.Visible = false;
                txtDistrict.Text = "";
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void btnDistrict_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable admin = (DataTable)Session["Admin"];
            string districtName = txtDistrict.Text.Trim();
            int regionID = Convert.ToInt32(ddlDistrict.SelectedValue);
            if (ddlDistrict.SelectedIndex != 0)
            {
                objDT = BO_Regions.Search(regionID);
                BO_Regions.Update(regionID,
                    Convert.ToInt32(objDT.Rows[0]["PARENT_ID"]), districtName, "",
                    1,
                   Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]), objDT.Rows[0]["CREATED_BY"].ToString(),
                    DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), true);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Sửa thành công !");
            }
            else
            {
                BO_Regions.Insert(
                    regionID, districtName, "",
                    1,
                    DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(),
                    DateTime.Now, "", true);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thêm thành công !");
            }
            loadRegion();
            lbRegion_SelectedIndexChanged(null, null);
            ddlProvince_SelectedIndexChanged(null, null);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            if (BO_Regions.Delete(Convert.ToInt32(ddlRegion.SelectedValue)))
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công !");
            else
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
            loadRegion();
            lbRegion_SelectedIndexChanged(null, null);
            ddlProvince_SelectedIndexChanged(null, null);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
    protected void btnDistrictDel_Click(object sender, EventArgs e)
    {
        try
        {
            if (BO_Regions.Delete(Convert.ToInt32(ddlDistrict.SelectedValue)))
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công !");
            else
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
            loadRegion();
            lbRegion_SelectedIndexChanged(null, null);
            ddlProvince_SelectedIndexChanged(null, null);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
    }
}
